import Vue from "vue";
import VueRouter from "vue-router";
// 分类打包
// const Login = () => import(/* webpackChunkName: "login_home_welome" */ "../components/login/Login.vue");
// const Home = () => import(/* webpackChunkName: "login_home_welome" */ "../components/home/users/Users.vue");
// const Welcome = () => import(/* webpackChunkName: "login_home_welome" */ "../components/home/welcome/Welcome.vue");

// const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ "../components/home/users/Users.vue");
// const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ "../components/home/power/rights/Rights.vue");
// const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ "../components/home/power/Roles/Roles.vue");

// const Cate = () => import(/* webpackChunkName: "Cate_Params" */ "../components/home/goods/cate/Cate.vue");
// const Params = () => import(/* webpackChunkName: "Cate_Params" */ "../components/home/goods/params/Params.vue");

// const GoodsList = () => import(/* webpackChunkName: "GoodsList_Add" */ "../components/home/goods/list/GoodsList.vue");
// const AddGoods = () => import(/* webpackChunkName: "GoodsList_Add" */ "../components/home/goods/list/children/AddGoods.vue");

// const Orders = () => import(/* webpackChunkName: "Order_Report" */ "../components/home/order/Orders.vue");
// const Reports = () => import(/* webpackChunkName: "Order_Report" */ "../components/home/report/Reports.vue");

// 独立打包
const Login = () => import("../components/login/Login.vue");
const Home = () => import("../components/home/Home.vue");
const Welcome = () => import("../components/home/welcome/Welcome.vue");

const Users = () => import("../components/home/users/Users.vue");
const Rights = () => import("../components/home/power/rights/Rights.vue");
const Roles = () => import("../components/home/power/Roles/Roles.vue");

const Cate = () => import("../components/home/goods/cate/Cate.vue");
const Params = () => import("../components/home/goods/params/Params.vue");

const GoodsList = () => import("../components/home/goods/list/GoodsList.vue");
const AddGoods = () => import("../components/home/goods/list/children/AddGoods.vue");

const Orders = () => import("../components/home/order/Orders.vue");
const Reports = () => import("../components/home/report/Reports.vue");

// 原始导入
// import Login from "../components/login/Login.vue";
// import Home from "../components/home/Home.vue";
// import Users from "../components/home/users/Users.vue";
// import Welcome from "../components/home/welcome/Welcome.vue";
// import Rights from "../components/home/power/rights/Rights.vue";
// import Roles from "../components/home/power/Roles/Roles.vue";
// import Cate from "../components/home/goods/cate/Cate.vue";
// import Params from "../components/home/goods/params/Params.vue";
// import GoodsList from "../components/home/goods/list/GoodsList.vue";
// import AddGoods from "../components/home/goods/list/children/AddGoods.vue";
// import Orders from "../components/home/order/Orders.vue";
// import Reports from "../components/home/report/Reports.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        component: Welcome,
      },
      {
        path: "/users",
        component: Users,
      },
      {
        path: "/rights",
        component: Rights,
      },
      {
        path: "/roles",
        component: Roles,
      },
      {
        path: "/categories",
        component: Cate,
      },
      {
        path: "/params",
        component: Params,
      },
      {
        path: "/goods",
        component: GoodsList,
      },
      {
        path: "/goods/add",
        component: AddGoods,
      },
      {
        path: "/orders",
        component: Orders,
      },
      {
        path: "/reports",
        component: Reports,
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 设置路由守卫验证token
router.beforeEach((to, from, next) => {
  // to 将访问哪一个路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数,表示放行
  if (to.path === "/login") {
    return next();
  }
  // 获取token验证其他页面访问
  const token = window.sessionStorage.getItem("token");
  if (!token) {
    return next("/login");
  }
  next();
});

export default router;
